var cityName = '',
	baseUrl = '/jiadianweixiu/v1/categories',
	pointlng = '',
	pointlat = '',
	cityNameFilter = '';

//调取参数--------------------------
function GetQueryString(name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  decodeURI(r[2]); return null;
}

//百度地图API定位功能----------------
$(function() {
	//获取具体城市的经纬度-------------------
	pointlng =GetQueryString("longitude");console.log(pointlng)
	pointlat=GetQueryString("latitude");
	//判断是否需要定位------------------
	if(cityName == '' || cityName == undefined) {
		if(pointlng && pointlat){
			localStorage.pointlng = pointlng;
			localStorage.pointlat = pointlat;
			var gc = new BMap.Geocoder();
			var point = new BMap.Point(localStorage.pointlng,localStorage.pointlat);
			gc.getLocation(point, function(rs){
			   var addComp = rs.addressComponents;
			   console.log(rs);
//			   alert(addComp.province +","+ addComp.city +","+ addComp.district);
			   cityNameFilter = addComp.province.replace(/[市省区]|特别行政区/g,'');
			   cityName = cityNameFilter;
			   //通过城市名获取数据
				getData(cityName);
			});
		}else{
			creatData();
		}	
	} else {
		creatData();
	};
})
//当未传入经纬度时------
	function creatData(){
		cityName = '上海';
		pointlng = '121.48789949';
		pointlat = '31.24916171';
		localStorage.pointlng = pointlng;
		localStorage.pointlat = pointlat;
		getData(cityName);
	}

//主要板块呈现---------
	function getData(cityName){
		var cusUrl = locationUrl + baseUrl;
		var sendData = {
			city : cityName
		};
		var type = 'GET' ;
		var opts = {
			'noMsg': true,
			success:function(data){
				console.log(data);
				$('.bigMain').html('')
				getMain(data);
				localStorage.removeItem('usertoken');
				localStorage.removeItem('userphone');
			}
		};
		KJin.ajax(cusUrl, sendData, opts,type);
	}
//主要内容加载----------
	function getMain(data){
		var mainStr = '',
			data = data.data,
			len = data.length ;console.log(len)
			for(var i = 0; i < len; i++){
//				if(i % 8 == 0 && i != 0){//此处为了兼容IE则删除了
//					mainStr += '<div class="mainList"><div class="mainListBtn" categoryId ="'+data[i].categoryId+'" cityId ="'+data[i].cityId+'"></div><img src=" '+data[i].categoryIcon+' " alt="" /></div><div class="LoadingWrap"><img src="img/downLoading.png"></div>';
//				}else{
					mainStr += '<div class="mainList"><div class="mainListBtn" categoryId ="'+data[i].categoryId+'" cityId ="'+data[i].cityId+'"></div><img src=" '+data[i].categoryIcon+' " alt="" /></div>'
//				}
			}
			$('.bigMain').append(mainStr);
			animateFn();
//				myIscroll.refresh();
	}
//点击跳转至详情页-------
	$('.bigMain').on('click','.mainListBtn',function(){
		var $that = $(this).attr('categoryId');console.log($that);
		var cityId = $(this).attr('cityId');console.log(cityId)
		window.location.href = 'html/details.html?categoryId='+$that+'&cityId='+cityId;
	})

//iScroll --------------
//jquery 动画
	function pullUpRefresh(){//alert('dfs')
		$('.LoadingWrap').children('img').stop().animate({
			top : '-30px',
			opacity : '0.7'
		},'slow');
		$('.LoadingWrap').children('img').animate({
			top : '-4px',
			opacity : '0.5',
			width : '15px',
			height:'10px'
		});
		$('.LoadingWrap').children('img').animate({
			top : '2px',
			opacity : '0.3',
			width : '8px',
			height:'4px'
		})
	};
	function animateFn(){
	  setInterval(function(){
//		$(".LoadingWrap img").animate({
//		  height:'toggle'
//		});
		$('.LoadingWrap').children('img').stop().animate({
			top : '-15px',
			transform : 'rotate(180deg)',
			opacity : '0.7'
		},'slow');
		$('.LoadingWrap').children('img').animate({
			top : '-4px',
			opacity : '0.2'
		},'slow');
	  },500)
   
  };
 //Iscroll 加载事件
//	var myIscroll = new IScroll('.mainWrap',{
//		mouseWheel: true,
//  		scrollbars: true,
//		probeType : 1,
//		scrollY : true,
//		preventDefault: true,
//		tap : true
//	})
	var topv = null;
	function load() {
		myScroll = new IScroll('.mainWrap', {
			probeType: 2,
			mouseWheel: true,
			click:true
		});
		myScroll.on('scrollStart', function() {
			console.log('start');
		})
		myScroll.on('scroll', function() {
			topv = this.y;
		})
		myScroll.on('scrollEnd', function() {
			/*******  此处为修改地方 ****/
			if( (this.y -100) <=this.maxScrollY || this.y==this.maxScrollY){
				console.log("上啦成功了");console.log(this.y);console.log(this)
				pullUpRefresh()
			}
			myScroll.refresh();
		});
	}
	//setTimeout(load, 200);
	
	
	//touchmove  事件
	var startX = startY = endX = endY = 0;  
        var dom = document.getElementById('main');
        //touchStart事件
        dom.addEventListener('touchstart',function(event){
            var touch = event.targetTouches[0];
            startX = touch.pageX;
            startY = touch.pageY;
			console.log('touchstart:'+startY)
        },false);
        //touchmove事件
        dom.addEventListener('touchmove',function(event){
            var touch = event.targetTouches[0];
            endX = touch.pageX;
            endY = touch.pageY;
            console.log('touchmove:'+endY);
            pullUpRefresh();
            $('.bigWrap .mainWrap').css('overflow','scroll')
        },false);
        //touchend事件
        dom.addEventListener('touchend',function(event){
           setInterval(function(){$('.LoadingWrap').remove()},1000);
        },false);
